package com.volley.help;

import java.io.File;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.net.http.AndroidHttpClient;
import android.os.Build;

import com.android.volley.Network;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.HttpClientStack;
import com.android.volley.toolbox.HttpStack;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageContainer;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.volley.help.ImageLoaderConfiguration.ImageSize;

public class ImageLoaderFactory {
	private static final ImageLoaderFactory sInstance = new ImageLoaderFactory();
	private ImageLoaderConfiguration configuration;

	public static ImageLoaderFactory getInstance() {
		return sInstance;
	}

	public void init(ImageLoaderConfiguration configuration) {
		this.configuration = configuration;
	}

	public RequestQueue newRequestQueue(Context context) {
		HttpStack stack = null;
		File cacheDir = new File(context.getCacheDir(), "volley");

		String userAgent = "volley/0";
		try {
			String packageName = context.getPackageName();
			PackageInfo info = context.getPackageManager().getPackageInfo(
					packageName, 0);
			userAgent = packageName + "/" + info.versionCode;
		} catch (NameNotFoundException e) {
		}

		if (stack == null) {
			if (Build.VERSION.SDK_INT >= 9) {
				stack = new HurlStack();
			} else {
				// Prior to Gingerbread, HttpUrlConnection was unreliable.
				// See:
				// http://android-developers.blogspot.com/2011/09/androids-http-clients.html
				stack = new HttpClientStack(
						AndroidHttpClient.newInstance(userAgent));
			}
		}

		Network network = new BasicNetwork(stack);

		RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir,
				configuration.maxCacheSize), network);
		queue.start();
		return queue;
	}

	public ImageContainer get(RequestQueue queue, String requestUrl,
			ImageListener imageListener, int maxWidth, int maxHeight) {
		if (queue == null) {
			queue = newRequestQueue(configuration.context);
		}
		ImageLoader loader = new ImageLoader(queue,
				new ImageLoader.ImageCache() {

					@Override
					public void putBitmap(String url, Bitmap bitmap) {
						configuration.mMemoryCache.put(url, bitmap);
					}

					@Override
					public Bitmap getBitmap(String url) {
						return configuration.mMemoryCache.get(url);
					}
				});
		// 检查宽高
		ImageSize size = configuration.getImageSize();
		if (maxWidth <= 0) {
			maxWidth = size.width;
		}
		if (maxHeight <= 0) {
			maxHeight = size.height;
		}
		return loader.get(requestUrl, imageListener, maxWidth, maxHeight);
	}

	public ImageContainer get(RequestQueue queue, String requestUrl,
			ImageListener imageListener) {
		ImageSize size = configuration.getImageSize();
		return get(queue, requestUrl, imageListener, size.width, size.height);
	}
}
